package com.learning.spring.cloud.alibaba.gateway.global.filter.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * ClassName: GlobalFilterConfig
 * Description: GlobalFilter 配置类
 * Date: 2019/5/31 10:42 【需求编号】
 * <p>
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@Configuration
public class GlobalFilterConfig {
    @Bean
    public GlobalFilter logGlobalFilter() {
        return new LogGlobalFilter();
    }

    /**
     * 由于 Spring Cloud Gateway 区分了过滤器逻辑执行的 “前置”和“后”阶段(参见如何工作)，
     * 优先级最高的过滤器是“前置”阶段的第一个阶段和“后”阶段的最后一个阶段。
     */
    @Slf4j
    static class LogGlobalFilter implements GlobalFilter, Ordered {

        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            log.info("custom global filter");
            return chain.filter(exchange);
        }

        @Override
        public int getOrder() {
            return -1;
        }
    }
}
